package cc.bukkitPlugin.banitem.manager;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import cc.bukkitPlugin.banitem.BanItem;
import cc.bukkitPlugin.banitem.BanItemEnume.ListType;
import cc.bukkitPlugin.util.plugin.ABukkitPlugin;
import cc.bukkitPlugin.util.plugin.AManager;
import cc.bukkitPlugin.util.plugin.INeedConfig;

public class PermissionMan extends AManager<BanItem> implements INeedConfig{

    private boolean mCheckItemId=false;
    private boolean mCheckItemDamage=false;

    public PermissionMan(BanItem pPlugin){
        super(pPlugin);
        this.mPlugin.registerConfigModel(this);
    }

    @Override
    public void setConfig(CommandSender pSender){
        ConfigManager tConfig=this.mPlugin.getConfigManager();
        this.mCheckItemId=tConfig.isCheckItemId();
        this.mCheckItemDamage=tConfig.isCheckItemDamage();
        ABukkitPlugin.info(pSender,">启用物品ID权限: "+this.mCheckItemId);
        ABukkitPlugin.info(pSender,">启用物品子ID权限: "+this.mCheckItemDamage);
    }

    /**
     * 检查玩家是否有接触该方块的权限
     * @param pPlayer 玩家
     * @param pBlock 方块
     * @param pListType 检查类型
     */
    public boolean hasPermission(Player pPlayer,Block pBlock){
        if(pPlayer==null)
            return false;
        if(pBlock==null||pBlock.getType()==Material.AIR)
            return true;
        if(this.hasHighPermission(pPlayer,ListType.BAN))
            return true;
        return this.hasLowPermission(pPlayer,ListType.BAN,pBlock.getTypeId(),pBlock.getData());
    }

    /**
     * 检查玩家是否有使用物品的权限
     * @param pPlayer 玩家
     * @param pItem 物品
     * @param pListType 检查类型
     */
    public boolean hasPermission(Player pPlayer,ItemStack pItem,ListType pListType){
        if(pPlayer==null)
            return false;
        if(pItem==null||pItem.getType()==Material.AIR)
            return true;
        if(this.hasHighPermission(pPlayer,pListType))
            return true;
        return this.hasLowPermission(pPlayer,pListType,pItem.getTypeId(),pItem.getDurability());
    }

    public boolean hasHighPermission(Player pPlayer,ListType pListType){
        // 全部免检查权限
        String node=this.mPlugin.getName()+".bypass";
        if(pPlayer.hasPermission(node))
            return true;
        // 单独忽视ban或use检查的权限
        String strType=pListType.name().toLowerCase();
        node+="."+strType;
        return pPlayer.hasPermission(node);
    }

    public boolean hasLowPermission(Player pPlayer,ListType pListType,int pTypeId,short pDamage){
        // 物品节点
        if(!this.mCheckItemId)
            return false;
        String strType=pListType.name().toLowerCase();
        String node=this.mPlugin.getName()+".bypass."+strType+"."+pTypeId;
        if(pPlayer.hasPermission(node))
            return true;
        // 耐久节点
        if(!this.mCheckItemDamage)
            return false;
        node=node+"@"+pDamage;
        return pPlayer.hasPermission(node);
    }

}
